//
//  AQueryViewController.m
//  AMSystem
//
//  Created by 徐晓斐 on 14-9-5.
//  Copyright (c) 2014年 GZDEU. All rights reserved.
//

#import "AQueryViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "AttendanceViewController.h"
#import "AMSystemManager.h"
#import "NSDate+convenience.h"
#import "JGProgressHUD.h"

@interface AQueryViewController ()
@property (nonatomic ,strong)NSUserDefaults *userDefaults;
@property (nonatomic , strong) UIView *tempview;
@property (nonatomic, assign) CGFloat cc_height;
@end

@implementation AQueryViewController
@synthesize tempview;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initTempViews];
    self.title  = @"考勤查询";
    self.userDefaults = [NSUserDefaults standardUserDefaults];
    
    VRGCalendarView *calendar = [[VRGCalendarView alloc]init];
    calendar.delegate = self;
    calendar.frame = CGRectMake(0, 70, self.view.bounds.size.width, self.view.bounds.size.width);
    [self.view addSubview:calendar];
    NSDate *date=[NSDate date];
    int month=[date month];
    int year=[date year];
    NSString *month_str=(month<=9)?[NSString stringWithFormat:@"0%d",month]:[NSString stringWithFormat:@"%d",month];
    
    [self httpRequestWithYearAndMonth:[NSString stringWithFormat:@"%d%@",year,month_str] AndCalendar:calendar];
    // Do any additional setup after loading the view.
}


-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month targetHeight:(float)targetHeight animated:(BOOL)animated{
//    NSLog(@"targetHeight:%f",targetHeight);
    if (month == (int)[NSDate date]) {
        NSArray *dates = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:5], nil];
        [calendarView markDates:dates];
    }
    [self calendarDescription:targetHeight];
}

-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month andYear:(int)year targetHeight:(float)targetHeight animated:(BOOL)animated{
//    NSLog(@"%d%d",year,month);
    NSString *month_str= (month<=9)? [NSString stringWithFormat:@"0%d",month]:[NSString stringWithFormat:@"%d",month];
    NSString *string=[NSString stringWithFormat:@"%d%@",year,month_str];
    [self httpRequestWithYearAndMonth:string AndCalendar:calendarView];
    [self calendarDescription:targetHeight];
}
-(void)calendarView:(VRGCalendarView *)calendarView dateSelected:(NSDate *)date{
    
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    //用[NSDate date]可以获取系统当前时间
    NSString *dateStr = [dateFormatter stringFromDate:date];
    
    [AMSystemManager interfaceAttendanceReportExactToDay:[self.userDefaults stringForKey:@"KEY"] serachDate:dateStr complation:^(id obj) {
        
        AttendanceViewController *attendVC = [[self storyboard] instantiateViewControllerWithIdentifier:@"Attendance Identifier"];
        attendVC.AttendanceArr = obj;
        UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] init];
        backBarButtonItem.title = @"";
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationItem.backBarButtonItem = backBarButtonItem;
        [self.navigationController pushViewController:attendVC animated:YES];
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)httpRequestWithYearAndMonth:(NSString *)yearMonth AndCalendar:(VRGCalendarView *)calendar{
    JGProgressHUD *progress=[[JGProgressHUD alloc]initWithStyle:JGProgressHUDStyleDark];
    progress.textLabel.text=@"正在获取考勤信息...";
    [progress showInView:self.view];

    NSString *userKey;
    if (self.pepoer.AReportname!=nil) {
        userKey=self.pepoer.AReportname;
    }else{
        userKey=[self.userDefaults objectForKey:@"KEY"];
    }
    [AMSystemManager interfaceAttendanceReportExactToMonth:userKey serachTime:yearMonth complation:^(id obj) {
        NSLog(@"http request seccess:%@",[obj objectForKey:@"list"]);
        [progress dismiss];
        if ([[obj objectForKey:@"list"] count]>0) {
            [calendar markDates:[obj objectForKey:@"list"]];
            [calendar setNeedsDisplay];
        }
    }];
}
-(void)initTempViews{
    tempview=[[UIView alloc]initWithFrame:CGRectMake(5, 200, self.view.frame.size.width-40, 200)];
    tempview.backgroundColor=[UIColor whiteColor];
    UILabel *labelInfor=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 200)];
    labelInfor.numberOfLines=0;
    labelInfor.font=[UIFont systemFontOfSize:12.0f];
    labelInfor.text=@"异常日期:\n正常日期:\n当天日期:\n其他日期:";
    
    UIView *view_color2=[[UIView alloc]initWithFrame:CGRectMake(60, 73, 50, 11)];
    ////背景rgb：251、229、221
    view_color2.backgroundColor=[UIColor colorWithRed:251/256.0 green:229/256.0 blue:221/256.0 alpha:1.0];
    UIView *view_color3=[[UIView alloc]initWithFrame:CGRectMake(60, 88, 50, 11)];
    
    //背景rgb：220、238、254
    view_color3.backgroundColor=[UIColor colorWithRed:220/256.0 green:238/256.0 blue:254/256.0 alpha:1.0];
    
    
    UIView *view_color1=[[UIView alloc]initWithFrame:CGRectMake(60, 103, 50, 11)];
    //255、249、183
    view_color1.backgroundColor=[UIColor colorWithRed:255/256.0 green:249/256.0 blue:183/256.0 alpha:1.0];
    
    UIView *view_color4=[[UIView alloc]initWithFrame:CGRectMake(60, 117, 50, 11)];
    view_color4.backgroundColor=[UIColor colorWithRed:240.0/256 green:240.0/256 blue:240.0/256 alpha:1.0];
    [tempview addSubview:labelInfor];
    [tempview addSubview:view_color1];
    [tempview addSubview:view_color2];
    [tempview addSubview:view_color3];
    [tempview addSubview:view_color4];
    [self.view addSubview:tempview];
    
}
-(void)calendarDescription:(CGFloat)height{
    _cc_height=height;
    [UIView beginAnimations:@"testanimation" context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationstop)];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    CGRect frames = tempview.frame;
    frames.origin.y = height+30;
    tempview.frame = frames;
    [UIView commitAnimations];
}


-(void)animationstop
{
    //传统方法
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    CGRect frames = tempview.frame;
    frames.origin.y =_cc_height+30;
    tempview.frame = frames;
    [UIView commitAnimations];
}

@end
